export const formatTime = (date, type) => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	let dateWord = ''
	if (type == 'YYMMDD') {
		dateWord = `${[year, month, day].map(formatNumber).join('-')}`
	} else if (type == 'hhmmss') {
		dateWord = `${[hour, minute, second].map(formatNumber).join(' : ')}`
	} else if (type == 'hhmm') {
		dateWord = `${[hour, minute].map(formatNumber).join(' : ')}`
	} else if (type == 'YMD') {
		dateWord = year + '年' + month + '月' + day + '日'
	} else {
		dateWord =
			`${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(' : ')}`
	}
	return dateWord
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

export const HowLongTime = (date) => {
	const timestamp = date;
	const timeDifference = new Date().getTime()/1000 - timestamp;
	const seconds = Math.floor(timeDifference);
	const minutes = Math.floor(seconds / 60);
	const hours = Math.floor(minutes / 60);
	const days = Math.floor(hours / 24);
	const years = Math.floor(days / 365);
	let dateWord = ''
	if (seconds < 60) {
		dateWord = `${seconds}秒前`
	} else if (minutes < 60) {
		dateWord = `${minutes}分钟前`
	} else if (hours < 24) {
		dateWord = `${hours}小时前`
	} else if (days < 365) {
		dateWord = `${days}天前`
	} else {
		dateWord = `${years}年前`
	}
	return dateWord
}